#pragma once
/// Copyright 2012, Alex Averian, Yonan Research
/// Released under BSD Licence



#include <d2d1.h>
#include <d2d1helper.h>
#pragma comment(lib, "D2d1.lib")


#include "qwic.h"
#include "qdirectwrite.h"

#pragma comment(lib, "shlwapi.lib")




namespace micro
{
namespace directx
{


// Common Typedefs
typedef D2D1_POINT_2F POINT_2F;



class QDirect2D
{
public:
    static HRESULT LoadBitmapFromFile(
        ID2D1RenderTarget* renderTarget,
        LPCTSTR uri,
        unsigned int destinationWidth,
        unsigned int destinationHeight,
        ID2D1Bitmap** bitmap
    );
    
    static HRESULT LoadBitmapFromResource(
        ID2D1RenderTarget* renderTarget,
        LPCTSTR resourceName,
        LPCTSTR resourceType,
        unsigned int destinationWidth,
        unsigned int destinationHeight,
        ID2D1Bitmap** bitmap
    );
    
	 

// 	static HRESULT DecodeImageFromThumbCache(
// 		IShellItem* shellItem,
// 		ID2D1RenderTarget* renderTarget,
// 		unsigned int thumbnailSize,
// 		ID2D1Bitmap** bitmap
// 		);
    
	//for DX applications
    static HRESULT GetGlobalFactory( ID2D1Factory** factory ); 

	//for applications with minimal DX needs
    static HRESULT GetLocalFactory( ID2D1Factory** factory ); 


    static POINT_2F GetMousePositionForCurrentDpi( LPARAM lParam )
    {
        static POINT_2F dpi = {96, 96}; // The default DPI
        
        QComPointer<ID2D1Factory> factory;
        
        if ( SUCCEEDED( GetGlobalFactory( &factory ) ) )
        {
            factory->GetDesktopDpi( &dpi.x, &dpi.y );
        }
        
        return D2D1::Point2F(
                   static_cast<int>( static_cast<short>( LOWORD( lParam ) ) ) * 96 / dpi.x,
                   static_cast<int>( static_cast<short>( HIWORD( lParam ) ) ) * 96 / dpi.y );
    }
    
    static POINT_2F GetMousePositionForCurrentDpi( float x, float y )
    {
        static POINT_2F dpi = {96, 96}; // The default DPI
        QComPointer<ID2D1Factory> factory;
        
        if ( SUCCEEDED( GetGlobalFactory( &factory ) ) )
        {
            factory->GetDesktopDpi( &dpi.x, &dpi.y );
        }
        
        return D2D1::Point2F( x * 96 / dpi.x, y * 96 / dpi.y );
    }
    
    static float ScaleValueForCurrentDPI( float value )
    {
        POINT_2F dpi = {96, 96};
        QComPointer<ID2D1Factory> factory;
        
        if ( SUCCEEDED( GetGlobalFactory( &factory ) ) )
        {
            factory->GetDesktopDpi( &dpi.x, &dpi.y );
        }
        
        return value * dpi.x / 96;
    }
    
    inline static bool HitTest( const D2D1_RECT_F& rect, const D2D1_POINT_2F& point )
    {
        return ( rect.left <= point.x && rect.right >= point.x && rect.top <= point.y && rect.bottom >= point.y );
    }
    
    inline static float GetRectWidth( const D2D1_RECT_F& rect )
    {
        return fabs( double( rect.right - rect.left ) );
    }
    
    inline static float GetRectHeight( const D2D1_RECT_F& rect )
    {
        return fabs( double( rect.bottom - rect.top ) );
    }
    
private:
	QDirect2D() {};
	~QDirect2D(){};
};



}
} //micro
 